package com.cluster.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.dto.SysCheckInfoDto;
import com.cluster.platform.mapper.SysCheckInfoMapper;
import com.cluster.platform.model.SysCheckInfo;
import com.cluster.platform.service.SysCheckInfoService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: SysCheckInfoServiceImpl
 */
@Service
public class SysCheckInfoServiceImpl implements SysCheckInfoService {

    @Autowired
    SysCheckInfoMapper sysCheckInfoMapper;


    @Override
    @Transactional
    public CommonResult<Object> batchSysCheck(Integer status, List<String> ids) {
        if (null == status || ids.isEmpty()) {
            return CommonResult.failed("未获取参数信息");
        }
        int batchUpdate = sysCheckInfoMapper.batchSysCheck(status, ids);
        if (batchUpdate < 0) {
            return CommonResult.failed("操作失败");
        }
        return CommonResult.success(batchUpdate);
    }

    @Override
    public CommonResult<Object> fetchList(SysCheckInfoDto sysCheckInfoDto) {
        if (null == sysCheckInfoDto.getPageNum() || null == sysCheckInfoDto.getPageSize()) {
            return CommonResult.failed("未获取参数信息");
        }
        PageHelper.startPage(sysCheckInfoDto.getPageNum(), sysCheckInfoDto.getPageSize());
        SysCheckInfo sysCheckInfo = BeanUtil.copyProperties(sysCheckInfoDto, SysCheckInfo.class);
        List<SysCheckInfo> sysCheckInfos = sysCheckInfoMapper.searchSysCheckInfo(sysCheckInfo);
        return CommonResult.success(CommonPage.restPage(sysCheckInfos));
    }

    @Override
    public CommonResult<Object> searchSysCheckInfo(SysCheckInfo sysCheckInfo) {
        List<SysCheckInfo> sysCheckInfos = sysCheckInfoMapper.searchSysCheckInfo(sysCheckInfo);
        if (sysCheckInfos.isEmpty()) {
            return CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(sysCheckInfos));
    }

    @Override
    @Transactional
    public CommonResult<Object> addSysCheckInfo(SysCheckInfo sysCheckInfo) {
        if (null == sysCheckInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        sysCheckInfo.setId(UUID.randomUUID().toString().replace("-", ""));
        sysCheckInfo.setStatus(1);
        sysCheckInfo.setInsertDate(new Date());
        int insertSelective = sysCheckInfoMapper.insertSelective(sysCheckInfo);
        if (insertSelective < 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success(sysCheckInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> updateSysCheckInfo(SysCheckInfo sysCheckInfo) {
        if (null == sysCheckInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        sysCheckInfo.setUpdateDate(new Date());
        int update = sysCheckInfoMapper.updateByPrimaryKeySelective(sysCheckInfo);
        if (update < 0) {
            return CommonResult.failed("更新失败");
        }
        return CommonResult.success(sysCheckInfo);
    }

    @Override
    @Transactional
    public CommonResult<Object> delSysCheckInfo(SysCheckInfo sysCheckInfo) {
        if (null == sysCheckInfo) {
            return CommonResult.failed("未获取参数信息");
        }
        sysCheckInfo.setStatus(0);
        sysCheckInfo.setUpdateDate(new Date());
        int update = sysCheckInfoMapper.updateByPrimaryKeySelective(sysCheckInfo);
        if (update < 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success(sysCheckInfo);
    }
}
